package com.hxzy.controller;

import com.hxzy.common.ExchangeConst;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

/**
 * @Author wy
 * @Description TODO
 * @Date 2024/2/23 16:10
 * @Version 1.0
 */
@RestController
public class DirectController {


    @Autowired
    private RabbitTemplate rabbitTemplate;


    // http://localhost:18081/direct/send?msg=red路由键&key=red          queue01和queueu02同时由到消息
    // http://localhost:18081/direct/send?msg=blue路由键&key=blue        queue01由到消息
    // http://localhost:18081/direct/send?msg=yellow路由键&key=yellow    queue02由到消息
    // http://localhost:18081/direct/send?msg=yellow路由键&key=abc       消息丢失
    @GetMapping(value = "/direct/send")
    public String sendDirect(String msg,String key){

        MessageProperties messageProperties = new MessageProperties();
        //设置消息唯一ID
        messageProperties.setMessageId(UUID.randomUUID().toString());

        //创建Message对象
        Message message = new Message(msg.getBytes(),messageProperties);

        this.rabbitTemplate.convertAndSend(ExchangeConst.QUEUE_DIRECT_EXCHANGE,key,message);
        return "direct发送成功";
    }

}
